package org.example;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Map;
import java.util.Random;

public class ArticleContentScraper_jsoup {

    private static final Logger logger = LoggerFactory.getLogger(ArticleContentScraper_jsoup.class);

    public String fetchArticleContent(String url) {
        logger.info("正在爬取文章内容：{}", url);

        try {
            // **第一步：发送请求获取 Cookie**
            Connection.Response initialResponse = Jsoup.connect(url)
                    .method(Connection.Method.GET)
                    .execute();

            // **获取服务器返回的 Cookie**
            Map<String, String> cookies = initialResponse.cookies();
            logger.info("获取到的 Cookies: {}", cookies);

            // **第二步：使用获取到的 Cookie 重新请求页面**
            Document doc = Jsoup.connect(url)
                    .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
                    .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
                    .header("Accept-Encoding", "gzip, deflate, br")
                    .header("Accept-Language", "zh-CN,zh;q=0.9")
                    .header("Connection", "keep-alive")
                    .cookies(cookies)  // **这里带上 Cookie**
                    .timeout(10000)
                    .get();


            System.out.println(doc);
            // **提取文章内容**
            String content = doc.select("div.index_cententWrap__Jv8jK").text();
            return content.isEmpty() ? "文章内容为空" : content;

        } catch (IOException e) {
            logger.error("发生异常: {}", e.getMessage(), e);
            return "无法获取内容";
        }
    }
}
